/*----------------------------------------------------------------------------
 * Copyright (c) <2016-2020>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/
#include "usart.h"
#include "stm32f4xx.h"
#include <los_hwi.h>

#include "ble.h"
#include "ble_uart.h"


//static USART_TypeDef*     s_pUSART = USART3;
//static uint32_t           s_uwIRQn = USART3_IRQn;

static USART_TypeDef*     s_pUSART = USART2;
static uint32_t           s_uwIRQn = USART2_IRQn;
UART_HandleTypeDef ble_uart_handle;


void ble_uart_irq(void)
{
    UART_HandleTypeDef *huart = &ble_uart_handle;
    uint32_t isrflags   = READ_REG(huart->Instance->SR);
    uint32_t cr1its     = READ_REG(huart->Instance->CR1);
    uint32_t cr3its     = READ_REG(huart->Instance->CR3);
    uint32_t errorflags = 0x00U;

    uint32_t ErrorCode  = HAL_UART_ERROR_NONE;
    uint8_t data;

    /* If no error occurs */
    errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
    if(errorflags == RESET)
    {
        /* UART in mode Receiver -------------------------------------------------*/
        if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
        {
            data = (uint8_t)(ble_uart_handle.Instance->DR & 0x00FF);
            ble_receive_byte(data);
            return;
        }
    }

    /* If some errors occur */
    if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || \
        ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
    {
        /* UART parity error interrupt occurred ----------------------------------*/
        if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
        {
            ErrorCode |= HAL_UART_ERROR_PE;
        }

        /* UART noise error interrupt occurred -----------------------------------*/
        if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
        {
            ErrorCode |= HAL_UART_ERROR_NE;
        }

        /* UART frame error interrupt occurred -----------------------------------*/
        if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
        {
            ErrorCode |= HAL_UART_ERROR_FE;
        }

        /* UART Over-Run interrupt occurred --------------------------------------*/
        if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
        {
            ErrorCode |= HAL_UART_ERROR_ORE;
        }

        /* Call UART Error Call back function if need be --------------------------*/
        if(ErrorCode != HAL_UART_ERROR_NONE)
        {
            /* UART in mode Receiver -----------------------------------------------*/
            if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
            {
                data = (uint8_t)(ble_uart_handle.Instance->DR & 0x00FF);
            }
            printf("ble_uart_irq ErrorCode:%lx %d\n",ErrorCode,__LINE__);
        }
    }
}


void ble_uart_init(void)
{
    //initialize the ble_uart
    ble_uart_handle.Instance = s_pUSART;
    ble_uart_handle.Init.BaudRate = 115200;
    ble_uart_handle.Init.WordLength = UART_WORDLENGTH_8B;
    ble_uart_handle.Init.StopBits = UART_STOPBITS_1;
    ble_uart_handle.Init.Parity = UART_PARITY_NONE;
    ble_uart_handle.Init.Mode = UART_MODE_TX_RX;
    ble_uart_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    ble_uart_handle.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&ble_uart_handle) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }

    //create a hardware interrupt to deal ble_uart_irq
    LOS_HwiCreate(s_uwIRQn, 3, 0, ble_uart_irq, 0);

    __HAL_UART_CLEAR_FLAG(&ble_uart_handle,UART_FLAG_TC);

    __HAL_UART_ENABLE_IT(&ble_uart_handle,UART_IT_ERR);
    __HAL_UART_ENABLE_IT(&ble_uart_handle,UART_IT_PE);
    //enable ble_uart UART_IT_RXNE interrupt
    __HAL_UART_ENABLE_IT(&ble_uart_handle,UART_IT_RXNE);//Receive Data register not empty interrupt

    __HAL_UART_ENABLE(&ble_uart_handle);
}

void ble_send_byte(unsigned char data)
{
    HAL_UART_Transmit(&ble_uart_handle,&data,1,100);
}

